﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CRM.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;

namespace CRM.Caches
{
    public class NationCache : BaseCache<Nation>
    {

        public NationCache(IMemoryCache cache) : base(cache)
        {
        }
        
        public Task<List<Nation>> GetNationList(MyDbContext dbContext)
        {
            return GetOrCreateAsync("NationList", entry =>
            {
                entry.SlidingExpiration = TimeSpan.FromHours(1);
                return dbContext.Nations.ToListAsync();
            });
        }
        
        public Task<List<Nation>> GetTree(MyDbContext dbContext)
        {
            return GetOrCreateAsync("NationTree", entry =>
            {
                entry.SlidingExpiration = TimeSpan.FromHours(1);
                return dbContext.Nations.Where(m => m.ParentId == 0)
                    .Include(m => m.Children)
                    .ToListAsync();
            });
        }

        // public Nation Get<TKey>(object id) where TKey : int
        // {
        //     return Get<Nation>(id);
        // }
        protected override Task<Nation> GetItem(object key, MyDbContext dbContext)
        {
            throw new NotImplementedException();
        }
    }
}